package org.michael.u.brg.i18n.litz;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

public class LiterizerMiniUi extends JFrame {
    private KeyMap keyMap;

    private class Editor extends JTextArea {

        protected void processKeyEvent(KeyEvent e) {
            for (KeyPunch keyPunch : keyMap.getKeyPunches()) {
                KeyStroke keyStroke = keyPunch.getKeyStroke();
                if (keyStroke.getKeyCode() == e.getKeyCode() && keyStroke.getModifiers() == e.getModifiers()
                        || keyPunch.getKeyChar() == e.getKeyChar() && (keyStroke.getModifiers() & 0xF) == e.getModifiers()) {
                    if (e.getID() == KeyEvent.KEY_PRESSED)
                        replaceSelection(keyPunch.getText());
                        //append(keyPunch.getText());

                    e.consume();
                    return;
                }
            }

            super.processKeyEvent(e);
        }

        public void unregisterKeyboardAction(KeyStroke aKeyStroke) {
            super.unregisterKeyboardAction(aKeyStroke);
        }

        private Editor() {
            super();
        }

    }


    public LiterizerMiniUi()
            throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
        initKeyMap();
        initUi();
    }

    private void initKeyMap()
            throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
        URL url = getClass().getClassLoader().getResource("at.xml");
        keyMap = KeyMap.readFromXmlFile(url.toURI());
    }

    private void initUi() {
        setTitle("MB Literizer, map: " + keyMap.getName());
        setDefaultCloseOperation(3);
        final Editor inputArea = new Editor();
        inputArea.setLineWrap(true);
        inputArea.setFont(keyMap.getFont());
        URL url = getClass().getClassLoader().getResource("litz.png");
        setIconImage((new ImageIcon(url)).getImage());
        getContentPane().add(inputArea);
        centerAndScale(this, 0.4F, 0.2F);
    }

    public static void main(String args[]) {
        try {
            LiterizerMiniUi mainWindow = new LiterizerMiniUi();
            mainWindow.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void centerAndScale(Component awtComponent, float widthScale, float heightScale) {
        assert awtComponent != null : "No component";
        assert widthScale > 0.0F && widthScale <= 1.0F && heightScale > 0.0F && heightScale <= 1.0F :
                String.format(
                        "Either the value of the width scale=%g and/or the height scale=%g are invalid; both expected to be >0 and <=1"
                        , widthScale, heightScale);
        GraphicsDevice currentDevice = awtComponent.getGraphicsConfiguration().getDevice();
        DisplayMode dm = currentDevice.getDisplayMode();
        int deviceWidth = dm.getWidth();
        int deviceHeight = dm.getHeight();
        awtComponent.setLocation(Math.round((float) deviceWidth * 0.5F * (1.0F - widthScale)), Math.round((float) deviceHeight * 0.5F * (1.0F - heightScale)));
        awtComponent.setSize(Math.round((float) deviceWidth * widthScale), Math.round((float) deviceHeight * heightScale));
    }

}
